/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bl.controllers;

import entities.ObjectFactory;
import entities.AreaType;
import entities.AreasOfStudyType;
import entities.AwardsType;
import entities.CvType;
import entities.CvsType;
import entities.Document;
import entities.DocumentType;
import entities.JobType;
import entities.JobsType;
import entities.LanguageRecordType;
import entities.LanguagesType;
import entities.MscType;
import entities.OtherMscType;
import entities.PersonalInfoType;
import entities.ProjectType;
import entities.ProjectsType;
import entities.ResearchType;
import entities.StudiesType;
import entities.TitleType;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author Spyros
 */
public class JAXBController {

    private ObjectFactory objectFactory;
    private DocumentType documentType;
    private DocumentJpaController jpaController;

    public JAXBController() {
        objectFactory = new ObjectFactory();
        documentType = objectFactory.createDocumentType();
    }

    public void save(Document document){
        marshal(document);
    }

    public void setAreasOfStudy(String area1Value, String area2Value, String area3Value){
        AreasOfStudyType areasOfStudyType = objectFactory.createAreasOfStudyType();
        AreaType area1 = new AreaType();
        area1.setName("e-Learning");
        area1.setValue(new Byte(area1Value));
        AreaType area2 = new AreaType();
        area2.setName("Network-Oriented Systems");
        area2.setValue(new Byte(area2Value));
        AreaType area3 = new AreaType();
        area3.setName("Networks");
        area3.setValue(new Byte(area3Value));
        ArrayList<AreaType> areaTypes = new ArrayList<AreaType>();
        areaTypes.add(area1);
        areaTypes.add(area2);
        areaTypes.add(area3);
        areasOfStudyType.getArea().addAll(areaTypes);
        documentType.setAreasOfStudy(areasOfStudyType);
    }

    public void setPersonalInfo(PersonalInfoType personalInfoType){
        documentType.setPersonalInfo(personalInfoType);
    }

    public void setStudies(Collection<TitleType> titleTypes){
        StudiesType studiesType = objectFactory.createStudiesType();
        studiesType.getTitle().addAll(titleTypes);
        documentType.setStudies(studiesType);
    }

    public void setProjects(Collection<ProjectType> projectTypes){
        ProjectsType projectsType = objectFactory.createProjectsType();
        projectsType.getProject().addAll(projectTypes);
        documentType.setProjects(projectsType);
    }

    public void setAwards(Collection<String> awards){
        AwardsType awardsType = objectFactory.createAwardsType();
        awardsType.getAward().addAll(awards);
        documentType.setAwards(awardsType);
    }

    public void setLanguages(Collection<LanguageRecordType> languageRecordTypes){
        LanguagesType languagesType = objectFactory.createLanguagesType();
        languagesType.getLanguageRecord().addAll(languageRecordTypes);
        documentType.setLanguages(languagesType);
    }

    public void setJobs(Collection<JobType> jobTypes){
        JobsType jobsType = objectFactory.createJobsType();
        jobsType.getJob().addAll(jobTypes);
        documentType.setJobs(jobsType);
    }

    public void setResearch(Collection<String> research){
        ResearchType researchType = objectFactory.createResearchType();
        researchType.getResearchActivity().addAll(research);
        documentType.setResearch(researchType);
    }

    public void setCvs(Collection<CvType> cvTypes){
        CvsType cvsType = objectFactory.createCvsType();
        cvsType.getCv().addAll(cvTypes);
        documentType.setCvs(cvsType);
    }

    public void setOtherMsc(Collection<MscType> mscTypes){
        OtherMscType otherMscType = objectFactory.createOtherMscType();
        otherMscType.getMsc().addAll(mscTypes);
        documentType.setOtherMsc(otherMscType);
    }

    public void setOtherInfo(String value){
        documentType.setOtherInfo(value);
    }

    private void marshal(Document document) {
        try {
            JAXBElement<DocumentType> gl = objectFactory.createDocument(documentType);
            JAXBContext jc = JAXBContext.newInstance("entities");
            Marshaller m = jc.createMarshaller();
            StringWriter stringWriter = new StringWriter();
            m.marshal(gl, stringWriter);
            //TODO instead of out DB create
            System.out.println(stringWriter.toString());
//            create(stringWriter.toString(), document);
        } catch (JAXBException jbe) {
            System.out.println(jbe.toString());
        }
    }

    private void create(String value, Document document){
        document.setDocument(value);
        jpaController = new DocumentJpaController();
        jpaController.create(document);
    }



}
